package com.view.adminMenu;

import com.run.RecordRun;
import com.model.Records;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class searchRecordsMenu {
    private JPanel mainPanel = new JPanel();
    private JLabel book_idLabel = new JLabel("图书编号:");
    private JLabel student_idLabel = new JLabel("学生学号:");
    private JLabel outdateLabel = new JLabel("借出时间");
    private JLabel backdateLabel = new JLabel("返还时间");
    private JTextField book_idField = new JTextField(10);
    private JTextField student_idField = new JTextField(10);
    private JTextField outdateField = new JTextField(10);
    private JTextField backdateField = new JTextField(10);
    private ScrollPane mainPane = new ScrollPane();
    private TextArea mainArea = new TextArea(100,100);
    private Box searchBox = Box.createHorizontalBox();
    private Box mainBox = Box.createVerticalBox();
    private JButton searchButton = new JButton("搜索");

    public JPanel init() {

        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(book_idLabel);
        searchBox.add(Box.createHorizontalStrut(10));
        searchBox.add(book_idField);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(student_idLabel);
        searchBox.add(Box.createHorizontalStrut(10));
        searchBox.add(student_idField);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(outdateLabel);
        searchBox.add(Box.createHorizontalStrut(10));
        searchBox.add(outdateField);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(backdateLabel);
        searchBox.add(Box.createHorizontalStrut(10));
        searchBox.add(backdateField);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(searchButton);

        mainBox.add(searchBox);
        mainBox.add(mainArea);

        mainPanel.add(mainBox);

        mainArea.append("\n");
        mainArea.append("学生学号          ");
        mainArea.append("图书编号                  ");
        mainArea.append("借出时间                  ");
        mainArea.append("返还时间          ");
        mainArea.append("备注          ");
        mainArea.append("\n");

        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Records records = new Records();
                records.setBook_id(book_idField.getText());
                records.setStudent_id(student_idField.getText());
                records.setOutdate(outdateField.getText());
                records.setBackdate(backdateField.getText());

                Connection conn = null;
                try {
                    conn = com.util.DBUtil.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                try {
                    ResultSet resultSet = new RecordRun().searchRecords(conn,records);
                    boolean isNull = true;
                    while(resultSet.next()){
                        mainArea.append("\n");
                        mainArea.append(resultSet.getString(1)+"                    ");
                        mainArea.append(resultSet.getString(2)+"                    ");
                        mainArea.append(resultSet.getString(3)+"                    ");
                        mainArea.append(resultSet.getString(4)+"                    ");
                        mainArea.append(resultSet.getString(5)+"                    ");
                        mainArea.append("\n");
                        isNull = false;
                    }
                    if(isNull == true) {
                        JOptionPane.showMessageDialog(null,"未查找到此条记录");
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        return mainPanel;
    }
}
